package com.eastfair.venueservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.consts.CommonConstants;
import com.eastfair.venueservice.dao.ReportVenueDrawingAuditHistoryMapper;
import com.eastfair.venueservice.dto.ReportVenueDrawingAuditHistoryDTO;
import com.eastfair.venueservice.entity.ReportVenueDrawingAudit;
import com.eastfair.venueservice.entity.ReportVenueDrawingAuditHistory;
import com.eastfair.venueservice.service.ReportVenueDrawingAuditHistoryService;
import com.eastfair.venueservice.vo.ReportVenueDrawingAuditHistoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 一键报馆图纸审核记录历史
 * </p>
 *
 * @author dq
 * @date 2022-09-16
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ReportVenueDrawingAuditHistoryServiceImpl extends SuperServiceImpl<ReportVenueDrawingAuditHistoryMapper, ReportVenueDrawingAuditHistory> implements ReportVenueDrawingAuditHistoryService {

    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ReportVenueDrawingAuditHistory> modelList) {
        modelList.forEach(s -> s.setIsDeleted(BusinessConstant.YES));
        return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(ReportVenueDrawingAuditHistory model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    @Override
    public List<ReportVenueDrawingAuditHistoryVO> listByReportVenueDrawingCategoryAuditHistoryId(Long reportVenueDrawingCategoryAuditHistoryId) {
        ReportVenueDrawingAuditHistoryDTO query = new ReportVenueDrawingAuditHistoryDTO();
        query.setReportVenueDrawingCategoryAuditHistoryId(reportVenueDrawingCategoryAuditHistoryId);
        List<ReportVenueDrawingAuditHistory> list = listReportVenueDrawingAuditHistory(query);
        return list
                .stream()
                .map(history -> BeanUtil.toBean(history, ReportVenueDrawingAuditHistoryVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ReportVenueDrawingAuditHistory> listReportVenueDrawingAuditHistory(ReportVenueDrawingAuditHistoryDTO historyDTO) {
        QueryWrapper<ReportVenueDrawingAuditHistory> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .lambda()
                .eq(ReportVenueDrawingAuditHistory::getIsDeleted, BusinessConstant.DELETE_NO)
                .eq(historyDTO.getReportVenueDrawingCategoryAuditHistoryId() != null,
                        ReportVenueDrawingAuditHistory::getReportVenueDrawingCategoryAuditHistoryId, historyDTO.getReportVenueDrawingCategoryAuditHistoryId())
        ;
        return list(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveReportVenueDrawingAuditHistory(ReportVenueDrawingAuditHistoryDTO historyDTO) {
        ReportVenueDrawingAuditHistory reportVenueDrawingAuditHistory = BeanUtil.toBean(historyDTO, ReportVenueDrawingAuditHistory.class);
        return save(reportVenueDrawingAuditHistory);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<ReportVenueDrawingAuditHistoryDTO> list, Long reportVenueDrawingCategoryAuditHistoryId) {
        if (list == null || list.isEmpty()) {
            return false;
        }
        list.forEach(historyDTO -> {
            historyDTO.setReportVenueDrawingCategoryAuditHistoryId(reportVenueDrawingCategoryAuditHistoryId);
            saveReportVenueDrawingAuditHistory(historyDTO);
        });
        return true;
    }

    @Override
    public List<ReportVenueDrawingAuditHistoryDTO> toReportVenueDrawingAuditHistoryDTOList(List<ReportVenueDrawingAudit> reportVenueDrawingAuditList) {
        CopyOptions copyOptions = CopyOptions
                .create()
                .setIgnoreProperties(CommonConstants.COMMON_IGNORE_PROPERTIES);
        return reportVenueDrawingAuditList
                .stream()
                .map(reportVenueDrawingAudit -> {
                    ReportVenueDrawingAuditHistoryDTO reportDrawingAuditHistoryDTO = BeanUtil.toBean(reportVenueDrawingAudit, ReportVenueDrawingAuditHistoryDTO.class, copyOptions);
                    reportDrawingAuditHistoryDTO.setReportVenueDrawingAuditId(reportVenueDrawingAudit.getId());
                    return reportDrawingAuditHistoryDTO;
                })
                .collect(Collectors.toList());
    }
}
